Stylebits Corner - Textboxes

level: any

Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index

Reviewing the Stylebits Parameters

The four parameters of stylebits are AddBit, RemoveBit, AddExtendedBit, RemoveExtendedBit. For a review of these four parameters, and an introduction to Stylebits in general, please read [Stylebits Corner - An Introduction].

Line Wrapping

"How can I get text to wrap in a textbox?" This question is frequently asked on the Liberty BASIC Forum. Line wrapping is easily achieved with a few stylebits.

    Nomainwin
    WindowWidth=200
    WindowHeight=160

    text1$ = "STYLEBITS allows you to change the style of a Liberty BASIC window or control."
    text2$ = "Add to or edit this text."

    Textbox #Main.txtbx, 0, 0, 190, 68
    Stylebits #Main.txtbx, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0
    Statictext #Main, text2$, 5, 80, 190, 30

    Open "LineWrap for LB" for Window_nf as #Main
    Print #Main, "Trapclose [endMain]"
    Print #Main, "Font Times_New_Roman 12 Bold"
    Print #Main.txtbx, text1$
    Wait

[endMain]
    Close #Main
    End

Go ahead and type in your own text. The text will wrap automatically as you type. It is possible to begin a new line in this word wrapping textbox, but you must use CTRL-ENTER rather than just ENTER.

If you don't like scroll bars, then use a resizable textbox. Substitute the above stylebits command with

Stylebits #main.txtbx, _WS_THICKFRAME OR_ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0

and let the user tug and pull the textbox all over the window. Be careful with this one. If your user shrinks the textbox and drags it behind another control, the textbox may become irretrievable. If you choose this effect, you may want to add some resize handling using the Locate and Refresh commands.

Limiting Text Entry

You can limit text length by omitting the vertical scroll bar. Without the ability to scroll, the textbox will only accept what can be visually seen. By carefully controlling the font size and the height of the textbox, user input can be contained.

    WindowWidth=200
    WindowHeight=160
    Nomainwin

    text1$ = "Doc and Sleepy and Grumpy and Sneezy and"
    text2$ = "Can you name the other 3?"

    Textbox #main.txtbx, 0, 0, 190, 68
    Stylebits #main.txtbx, _ES_MULTILINE, _ES_AUTOHSCROLL OR _ES_AUTOVSCROLL, 0, 0
    Statictext #main, text2$, 5, 80, 190, 30

    Open "LineWrap for LB" for Window_nf as #main
    Print #main, "Trapclose [endDemo]"
    Print #main, "Font Times_New_Roman 12 Bold"
    Print #main.txtbx, text1$

    Wait

[endDemo]
	Close #main
	End

By removing the multiline stylebit you can control the length of a single line of text input.

Stylebits #main.txtbx, 0, _ES_AUTOHSCROLL OR _ES_AUTOVSCROLL, 0, 0

"The textbox is nice, but I really need a text editor. What stylebits will add word wrapping to the text editor?" Unfortunately, the text editor is a [widget] and widgets do not respond well to stylebits. If your program requires a word wrapping text editor, consider downloading Alyce Watson's [Text Editor by API]. This API modified textbox works just like a real text editor, it's compatible with Liberty BASIC v3.x as well as v4.x, and, best of all, like so many of [Alyce's Liberty BASIC contributions], it's free.

Scrolling vs Wrapping

It isn't always desirable to wrap text. If you anticipate text will extend beyond the limits of your textbox but don't want to increase the height of the textbox, consider adding a horizontal scroll bar.

Stylebits #main.txtbx, _WS_HSCROLL, 0, 0, 0

Textbox Borders

Textbox borders respond to window style (_WS_) stylebits, but may require unique combinations. While

Stylebits #main.gb, 0, _WS_BORDER, 0, 0

will remove the border of a graphic box, the same effect with a textbox can only be achieved with the removal of an extended style:

Stylebits #main.txtbx, 0, _WS_BORDER, 0, _WS_EX_CLIENTEDGE

Thanks to [Mike Bradbury (See Reply #3)] for posting this solution for removing the textbox border.

How about adding a horizontal scroll bar and removing the border? Here's a different look.

Stylebits #main.txtbx, _WS_HSCROLL, _WS_BORDER, 0, _WS_EX_CLIENTEDGE

Rather than removing a border, stylebits might be used to draw a more defined border, giving a distinct 3 - D effect. _WS_EX_DLGMODALFRAME draws a raised border

Stylebits #main.txtbx, 0, 0, _WS_EX_DLGMODALFRAME, 0

while _WS_EX_STATICEDGE draws a "step into" sunken border

Stylebits #main.txtbx, 0, 0, _WS_EX_STATICEDGE, 0

Restricting User Input

Edit style (_ES_) stylebits can be used to restrict or modify user input. Some examples include

Numbers Only (Unfortunately, commas and periods are not accepted. Forget about using this style if decimals are involved.)

Stylebits #main.txtbx, _ES_NUMBER, 0, 0, 0

Password (The asterisk is the default. This character can be changed by using the SetPasswordChar function. Note that _ES_MULTILINE must be included as a RemoveBit parameter.)

Stylebits #main.txtbx, _ES_PASSWORD, _ES_MULTILINE, 0, 0

Uppercase / Lowercase (Converts all user input to uppercase / lowercase.)

Stylebits #main.txtbx, _ES_UPPERCASE, 0, 0, 0
Stylebits #main.txtbx, _ES_LOWERCASE, 0, 0, 0

Left / Center / Right Justify (Can be used in either single line textboxes or multiline textboxes.)

Stylebits #main.txtbx, _ES_LEFT, 0, 0, 0
Stylebits #main.txtbx, _ES_CENTER, _ES_MULTILINE, 0, 0
Stylebits #main.txtbx, _ES_RIGHT, 0, 0, 0

Read Only (Will not accept user input. Issuing a Disable command later in the program will 'gray out' any text in the textbox, but Enable will not reverse the _ES_READONLY style.)

Stylebits #main.txtbx, _ES_READONLY, 0, 0, 0

A word about Dialog windows. Stylebits sometimes act differently when applied to controls in a Dialog or Dialog_Modal window. If you are experimenting with Stylebits, try them first in a regular window.

The Stylebits Corner Series

What else can be done with stylebits? In the coming months the STYLEBITS command will be used to, among other things,


Eager to try Stylebits with buttons and other window controls? Can't wait for the next newsletter? Good news! The best way to eplore and assign stylebit commands is with the Stylebits Wizard option found in Alyce Watson's [Liberty BASIC Workshop].


DEMO

The file stylebitsDemo3.bas is included in the zipped archive of this newsletter.


Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index